package org.jfonia.connect5.collections;

import org.jfonia.connect5.basics.Node;
import org.jfonia.connect5.basics.Observer;


/**
 * @author wijnand.schepens@hogent.be
 */
public class ListSynchronizer<S, T> implements Observer
{
	protected ArrayListNode<S> list1;
	protected ArrayListNode<T> list2;

	public ListSynchronizer(ArrayListNode<S> set1, ArrayListNode<T> set2)
	{
		this.list1 = set1;
		this.list2 = set2;

		set1.getAdditionNode().addObserver(this);
		set1.getRemovalNode().addObserver(this);
		set2.getAdditionNode().addObserver(this);
		set2.getRemovalNode().addObserver(this);
	}

	public void onNotify(Object source)
	{
		if (source == list1.getAdditionNode())
			onAddToSet1(list1.getAdditionNode().getValue());
		else if (source == list1.getRemovalNode())
			onRemoveFromSet1(list1.getRemovalNode().getValue());
		else if (source == list2.getAdditionNode())
			onAddToSet2(list2.getAdditionNode().getValue());
		else if (source == list2.getRemovalNode())
			onRemoveFromSet2(list2.getRemovalNode().getValue());
	}

	protected void onAddToSet1(int index)
	{
		list2.add(index, transformTo2(index), this);
	}

	protected void onAddToSet2(int index)
	{
		list1.add(index, transformTo1(index), this);
	}

	protected void onRemoveFromSet1(int index)
	{
		list2.remove(index, this);
	}

	protected void onRemoveFromSet2(int index)
	{
		list1.remove(index, this);
	}


	protected T transformTo2(int index)
	{
		return null;
	}

	protected S transformTo1(int index)
	{
		return null;
	}

}
